﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public class Rectangle : UIElement
    {

        public override void Init()
        {
            base.Init();
        }
       
        public override void OnEnter()
        {
            base.OnEnter();
            this.GenSelfMesh();
            GenRender();
            UpdateToRender();
        }

        public override void GenSelfMesh()
        {
            SimpleFill();
        }

        void SimpleFill() 
        {
            this.color = SGTools.ColorFromHTML(this.PropertyValue<string>("Color"));
            this.mGeom = new UIGeometry();
            this.mGeom.verts = new List<Vector3>();
            this.mGeom.cols = new List<Color>();
            //Vector
            Vector4 size = this.GetElementSize();
            this.mGeom.verts.Add(new Vector3(size.x,size.y,0)); //minx,miny
            this.mGeom.verts.Add(new Vector3(size.z,size.y,0)); //maxx,miny
            this.mGeom.verts.Add(new Vector3(size.x,size.w,0)); //minx,maxy
            this.mGeom.verts.Add(new Vector3(size.z,size.w,0)); //maxx,maxy
            //Color
            this.mGeom.cols.Add(this.color);
            this.mGeom.cols.Add(this.color);
            this.mGeom.cols.Add(this.color);
            this.mGeom.cols.Add(this.color);
        }

        void SetColor(Color32 color) 
        {
            if (mGeom != null)
            {
                this.mGeom.cols[0] = this.color;
                this.mGeom.cols[1] = this.color;
                this.mGeom.cols[2] = this.color;
                this.mGeom.cols[3] = this.color;
                gameObject.GetComponent<MeshFilter>().sharedMesh.colors = this.mGeom.cols.ToArray();
            }
        }
        void GenRender() 
        {
            MeshRenderer render = this.gameObject.AddComponent<MeshRenderer>();
            this.gameObject.AddComponent<MeshFilter>();
            render.material = new Material(Shader.Find("SGUIShader/Texture"));
        }


        
    }
}
